<?php


namespace WXAPI\Logic;
use Think\Model\RelationModel;
/**
 * 小程序订阅消息
 */
class WxsendLogic extends RelationModel{
    protected $appid;
    protected $secret;

    public function __construct()
    {
//        $plugin = M('wx_user')->find();
        $this->appid = 'wx5427cce6d89fe90a';
        $this->secret = 'b15ca70b1193e603d3f7cc276f91d94c';

    }

    /**
     * 小程序订阅消息发送消息
     * @param string $openid
     * @param int $board_id 模板id 1=>支付成功2=》发货通知3=》订单取消通知4=》自提信息提交5=>确认收货6=》下单成功7=>修改价格
     * @return array $result
     */
    public function sendSubscribeMessage($openid,$board_id,$template_id,$SendData){
        $access_token=$this->getAccessToken()["access_token"];

        $data = [] ;
        if($board_id == 1){

            $data['data'] = [
                "character_string1"=>[
                    'value' => $SendData['order_sn']
                ],
                "name2"=>[
                    'value' => $SendData['nickname']
                ],
                'date4'=>[
                    'value'=>$SendData['time']
                ],
                'thing7'=>[
                    'value'=>$SendData['address']
                ],
                'amount9'=>[
                    'value'=>$SendData['price']
                ]
            ];
            $data['page'] = "pages/order_details/order_details?id=".$SendData['order_id'].'&user_id='.$SendData['user_id'];
        }elseif($board_id == 2){
            $data['data'] = [
                "character_string1"=>[
                    'value' => $SendData['order_sn']
                ],
                "name2"=>[
                    'value' => $SendData['nickname']
                ],
                "date4"=>[
                    'value' => $SendData['time']
                ],
                'thing6'=>[
                    'value'=>$SendData['store_name']
                ],
                'amount9'=>[
                    'value'=>$SendData['price']
                ],
            ];
            $data['page'] = "pages/order_details/order_details?id=".$SendData['order_id'].'&user_id='.$SendData['user_id'];
        }

        $data['touser'] = $openid;
        $data['template_id'] = $template_id;
//        $data['miniprogram_state'] = 'developer';
//        var_dump($data);
        $result = $this->sendFunction($access_token,$data);
        return $result;
    }

    /**
     * 获取access_token
     * @return array $output
     */
    public function getAccessToken()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);

        return $output;
    }

    /**
     * 发送
     * @param $access_token
     * @param $data
     * @return array $output
     */
    public function sendFunction($access_token,$data)
    {
        $data  = json_encode($data);
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token;
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output,true);
    }
}
